package Dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import View.RequestTransferView;

import Entity.Transfer;

public class RequestTransferDao {
	public static List<Transfer> searchApproval() throws IOException,
			ClassNotFoundException, SQLException {
		String sql = "select * from Transfer where Approval = 'No'";
		List<Transfer> transferList = new Vector<Transfer>();
		Connection con = DaoUtil.getConnection();
		PreparedStatement stm = DaoUtil.getPreparedStatement(con, sql);
		ResultSet rs = stm.executeQuery();
		while (rs.next()) {
			transferList.add(new Transfer(rs.getInt("ID"), rs.getString("EmpNumber"),
					rs.getString("EmpName"), rs.getString("FromLocation"), rs.getString("ToLocation"), rs.getString("FromProject"),rs.getString("ToProject"), rs.getString("FromDept"),rs.getString("ToDept")));
//			System.out.print(rs.getInt("ID") + rs.getString("EmpNumber")
//					+ rs.getString("EmpName") + rs.getString("ToLocation")
//					+ rs.getString("ToProject"));
		}

		DaoUtil.closeConnection(con);
		return transferList;

	}

	public static Transfer selectTableLoad(int row) throws IOException,
			ClassNotFoundException, SQLException {
		String sql = "select * from Transfer where ID = ? ";
		Transfer transfer = new Transfer(row, null, null, null, null, null);
		Connection con = DaoUtil.getConnection();
		PreparedStatement stm = DaoUtil.getPreparedStatement(con, sql);
		stm.setInt(1, row);
		ResultSet rs = stm.executeQuery();
		while (rs.next()) {
			transfer = new Transfer(rs.getInt("ID"), rs.getString("EmpNumber"),
					rs.getString("EmpName"), rs.getString("FromLocation"), rs.getString("ToLocation"), rs.getString("FromProject"),rs.getString("ToProject"), rs.getString("FromDept"),rs.getString("ToDept"));
			// System.out.print(rs.getInt("ID")+ rs.getString("EmpNumber")+
			// rs.getString("EmpName")+ rs.getString("ToLocation")
			// +rs.getString("ToProject"));
		}

		DaoUtil.closeConnection(con);
		return transfer;
	}
	public void Approval(int ID) throws IOException, ClassNotFoundException, SQLException{
		String sql = "update Transfer set Approval = 'Yes', Agree = 'Yes' where ID = ?";
		Connection con = DaoUtil.getConnection();
		PreparedStatement stm = DaoUtil.getPreparedStatement(con, sql);
		stm.setInt(1, ID);
		stm.executeUpdate();
	}
	public void Refuse(int ID) throws IOException, ClassNotFoundException, SQLException{
		String sql = "update Transfer set Approval = 'Yes', Agree = 'No' where ID = ?";
		Connection con = DaoUtil.getConnection();
		PreparedStatement stm = DaoUtil.getPreparedStatement(con, sql);
		stm.setInt(1, ID);
		stm.executeUpdate();
	}
}
